'use strict';
define(['app'], function(app)
{
    //命名空间
    namespace('click.action').function = 'click.action.function';

    var HomeViewController = BaseController.extend({

        //服务初始化
        _notifications: null,
        setTimeout: null,

        //初始化事件
        bind1: null,
        bind2: null,

        //初始全局变量
        _data1: 'welcome1',
        _data2: 'welcome2',

        init: function($scope,$timeout,Notifications,homeEvent){

            //注入服务
            this.setTimeout = $timeout;

            this._notifications = Notifications;
            this.homeEvent = homeEvent;

            //TODO:绑定事件服务,被继承要加上判断，想办法简略
            if(this.homeEvent){
                this.bind1 = this.homeEvent._bind1;
                this.bind2 = this.homeEvent._bind2;
            }

            this._super($scope);
            $scope.page =
            {
                heading: $scope.page
            };
            //给后面继承的控制器调用
            $scope.extend = '来自主页的继承';
        },

        defineListeners:function(){
            this._super();
            //2个事件写入事件日志，默认会运行2个bind事件
            this._notifications.addEventListener(click.action.function,this.bind1.bind(this));
            this._notifications.addEventListener(click.action.function,this.bind2.bind(this));
        },

        //驱动函数
        _clickAction: function(){
            this._notifications.notify(click.action.function);
        },

        defineScope:function(){
            //绑定的模型（初始事件绑定）
            this.$scope.page = "I love OpenCode.ca";
            this.$scope.fun = this._clickAction.bind(this);
        },

        destroy:function(){
            this._notifications.removeEventListener(click.action.function,this.bind1.bind(this));
            this._notifications.removeEventListener(click.action.function,this.bind2.bind(this));
        }
    });

    //注入
    (function(){
        HomeViewController.$inject = ['$scope','$timeout','Notifications','homeEvent'];

        app.controller('HomeViewController',function($scope,$timeout,Notifications,homeEvent){
            new HomeViewController($scope,$timeout,Notifications,homeEvent);
        });
    })();

    //暴露控制器给下面页面继承
    return HomeViewController;
});